package com.cskaoyan.wordmemorize.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.VocCategoryConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.BookCategoryService;
import org.apache.ibatis.executor.BatchExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author 顾海龙
 * @version 1.0
 */
@Service
public class BookCategoryServiceImpl extends ServiceImpl<VocCategoryMapper, VocCategoryDO> implements BookCategoryService {
    @Autowired
    VocCategoryMapper vocCategoryMapper;

    @Autowired
    VocCategoryConverter vocCategoryConverter;

    @Autowired
    VocBookMapper vocBookMapper;

    //获取指定书籍类目
    @Override
    public VocCategoryDTO getCategory(String categoryId) throws BatchExecutorException {
        VocCategoryDO byId = this.getById(categoryId);
        VocCategoryDTO vocCategoryDTO = vocCategoryConverter.bookCategoryEntity2DTO(byId);
        return vocCategoryDTO;
    }

    //添加书籍类名
    @Override
    public void addCategory(VocCategoryDO vocCategoryDO) throws BatchExecutorException{
        this.save(vocCategoryDO);
    }

    //修改书籍类目
    @Override
    public void updateCategory(VocCategoryDO vocCategoryDO) throws BatchExecutorException{
        UpdateWrapper<VocCategoryDO> vocCategoryDOUpdateWrapper = new UpdateWrapper<>();
        vocCategoryDOUpdateWrapper.eq("id", vocCategoryDO.getId());
        this.update(vocCategoryDO, vocCategoryDOUpdateWrapper);
        // 修改类目下书籍
        String categoryName = vocCategoryDO.getCategoryName();
        Long id = vocCategoryDO.getId();
        LambdaUpdateWrapper<VocBookDO> eq = new LambdaUpdateWrapper<VocBookDO>()
                .eq(VocBookDO::getBcId, id);
        VocBookDO vocBookDO = new VocBookDO();
        vocBookDO.setBcName(categoryName);
        int update = vocBookMapper.update(vocBookDO, eq);
    }


    //分页查询书籍类目
    @Override
    public PageDTO<VocCategoryDTO> getCategoryList(PageRequest pageRequest) throws BatchExecutorException{
        PageDTO<VocCategoryDTO> vocCategoryDTOPageDTO;
        QueryWrapper<VocCategoryDO> vocCategoryDOQueryWrapper = new QueryWrapper<>();
        if ((pageRequest.getKeyword() != null) && !("".equals(pageRequest.getKeyword()))){
            vocCategoryDOQueryWrapper.like("category_name",pageRequest.getKeyword());
        }
        //判断是否有进行分页查询
        if(pageRequest.getPageNum() == null || pageRequest.getPageNum() < 0){
            List<VocCategoryDO> list = this.list(vocCategoryDOQueryWrapper);
            vocCategoryDTOPageDTO = vocCategoryConverter.bookCategoryList2Page(list,(long)list.size());
        }
        else {
            Page<VocCategoryDO> vocCategoryDOPage = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
            Page<VocCategoryDO> vocCategoryDOPage1 = vocCategoryMapper.selectPage(vocCategoryDOPage, vocCategoryDOQueryWrapper);
            vocCategoryDTOPageDTO = vocCategoryConverter.bookCategoryList2Page(vocCategoryDOPage1.getRecords(),vocCategoryDOPage1.getTotal());
        }
        return vocCategoryDTOPageDTO;
    }


    //删除指定书籍类目
    @Override
    public void deleteCategory(String categoryId) throws BatchExecutorException {
        this.removeById(categoryId);
    }

    //批量删除书籍类目
    @Override
    public void deleteCategoryByList(List<String> ids) throws BatchExecutorException{
        this.removeByIds(ids);
    }
}
